package com.easonji.admin.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.easonji.admin.form.AppForm;
import com.easonji.admin.manager.AppManager;
import com.easonji.admin.query.AppQuery;
import com.easonji.admin.service.AppService;
import com.easonji.core.annotion.BussinessLog;
import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easonji.common.base.controller.BaseController;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.constant.Consts;
import com.easonji.common.constant.dictmap.AppDict;
import com.easonji.common.util.StringUtils;

/**
 * @ClassName: AppController
 * @Description: 应用控制器
 * @Author: LiuJun
 * @date: 2018/07/30 14:07:42
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Controller
@RequestMapping("/app")
public class AppController extends BaseController {

    /**
     * 页面前缀
     */
    private final static String PREFIX = "system/app/";

    @Resource
    private AppService appService;

    @Resource
    private AppManager appManager;

    /**
     * 跳转到应用首页
     */
    @Permission(Consts.ADMIN_ROLE_NAME)
    @GetMapping("")
    public String index() {
        return PREFIX + "app";
    }

    /**
     * 跳转到添加应用页面
     */
    @Permission(Consts.ADMIN_ROLE_NAME)
    @GetMapping("/to_add")
    public String toAdd() {
        return PREFIX + "app_add";
    }

    /**
     * 跳转到修改应用页面
     */
    @Permission(Consts.ADMIN_ROLE_NAME)
    @GetMapping("/to_update/{appId}")
    public String toUpdate(@PathVariable Integer appId, Model model) {
        Object result = appService.queryApp(appId);
        LogObjectHolder.me().set(result);
        model.addAttribute("app", result);
        return PREFIX + "app_edit";
    }

    /**
     * 查询应用列表
     */
    @Permission(Consts.ADMIN_ROLE_NAME)
    @ResponseBody
    @PostMapping("/list")
    public Object queryAppList(AppQuery appQuery) {
        Object result = appService.queryAppList(appQuery);
        return result;
    }

    /**
     * 添加应用
     */
    @ResponseBody
    @Permission(Consts.ADMIN_ROLE_NAME)
    @BussinessLog(value = "添加应用", key = "name", dict = AppDict.class)
    @PostMapping("/add")
    public Response addApp(@Valid AppForm appForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = appService.saveApp(appForm);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.SAVE_SUCCESS);
    }

    /**
     * 修改应用
     */
    @ResponseBody
    @Permission(Consts.ADMIN_ROLE_NAME)
    @BussinessLog(value = "修改应用", key = "name", dict = AppDict.class)
    @PostMapping("/update")
    public Response updateApp(@Valid AppForm appForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = appService.updateApp(appForm);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 删除应用
     */
    @ResponseBody
    @Permission(Consts.ADMIN_ROLE_NAME)
    @BussinessLog(value = "删除应用", key = "name", dict = AppDict.class)
    @PostMapping("/remove")
    public Response removeApp(@RequestParam Integer appId) {
        if (StringUtils.isEmpty(appId)) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        // 缓存被删除的应用名称
        LogObjectHolder.me().set(appManager.getAppNameById(appId));
        boolean result = appService.removeApp(appId);
        if (!result) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.REMOVE_SUCCESS);
    }
}
